package com.zijie.shopviewapi.view.api;

import com.zijie.core.util.JsonResult;
import com.zijie.view.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

import static java.time.LocalTime.now;

@RestController
@RequestMapping("/api/v1/view")
public class ViewApi {
    private ViewService viewService ;

    @Autowired
    public void setViewService(ViewService viewService) {
        this.viewService = viewService;
    }

    @GetMapping("/count")
    public Map<String,Object> count(){
        Map<String,Object> map  = new HashMap<>();
        map.put("countOrders",viewService.countOrders());
        map.put("countMembers",viewService.countMembers());
        map.put("prices",viewService.countPrices());
        return map;
    }
    @GetMapping("/hotItems")
    public ResponseEntity<JsonResult<?>> hotItems(){
        List<Map<String,Object>> list = viewService.hotItems();
        return ResponseEntity.ok(JsonResult.success( list));
    }
    @GetMapping("/orderCount")
    public ResponseEntity<JsonResult<?>> orderCount(@RequestParam("startDate") String  startDate,@RequestParam("endDate") String endDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);
        List<Map<String,Object>> list = viewService.orderCountGroupByDate(start,end);
        return ResponseEntity.ok(JsonResult.success( list));
    }
    @GetMapping("/priceCount")
    public ResponseEntity<JsonResult<?>> priceCount(@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);
        List<Map<String,Object>> list = viewService.priceCountGroupByDate(start,end);
        return ResponseEntity.ok(JsonResult.success( list));
    }
//    @GetMapping("/countMembers")
//    public Long countMembers()
//    {
//        return viewService.countMembers();
//    }
//    @GetMapping("/countOrders")
//    public Long countOrders(){
//        return viewService.countOrders();
//    }
//    @GetMapping("/countPrices")
//    public Double countPrices(){
//        return viewService.countPrices();
//    }
}
